
define(['config/appregister',"business/personalcenter/service/shoppingHttp","business/services/detailNavComp","jquery"
],function(app,httpServices){
    app.controller('shoppingCarCtrl',function($scope,$state,$http){
        //初始化数据
        $scope.isDel=false;
        //获取购物车信息
        getShoppingList();
        function getShoppingList(){
            var req = {
                type:'GET',
                uri:'getList',
                data:{
                    all:true
                }
            };
            httpServices.shoppingHttpService(req).success(function(response){
                $scope.$apply(function(){
                    try {
                        $scope.shoppingList=response.data.list;
                        for(var i=0;i<$scope.shoppingList.length;i++){
                            $scope.shoppingList[i].flag=false;
                            for(var j=0;i<$scope.shoppingList[i].cartList.length;j++){
                                $scope.shoppingList[i].cartList[j].cartFlag=false;
                            }
                        }
                    }
                    catch(e) {
                        console.error('error');
                    }
                    console.log($scope.shoppingList);
                })
            }).error(function(response){

            })
        }

        //计算价格 课程数量
        function totalPrice(){
            $scope.price=0;
            $scope.num=0;
            angular.forEach($scope.shoppingList, function(data,index){
                angular.forEach($scope.shoppingList[index].cartList, function(info,i){
                    if(info.flag&&info.scId){
                       $scope.price+=info.money;
                        $scope.num+=1;
                    }
                });
            });
        }
        //选择单个课程
        $scope.singleSelect=function(){
            totalPrice();
        };
        //点击一个公司全选按钮选该公司所有课程
        $scope.companySelectAll=function(item){
            if(item.flag){
                angular.forEach(item.cartList, function(data,index){
                    data.flag=true;
                });
            }else{
                angular.forEach(item.cartList, function(data,index){
                    data.flag=false;
                });
            }
            totalPrice();
        };

        //全选所有课程
        $scope.selectAll=function(){
            if($scope.all){
                angular.forEach($scope.shoppingList, function(data,index){
                    data.flag=true;
                    angular.forEach($scope.shoppingList[index].cartList, function(data,index){
                        data.flag=true;
                    });
                });
            }else{
                angular.forEach($scope.shoppingList, function(data,index){
                    data.flag=false;
                    angular.forEach($scope.shoppingList[index].cartList, function(data,index){
                        data.flag=false;
                    });
                });
            }
            totalPrice();
        };

        //添加订单
        $scope.addOrder = function(){
            if(localStorage.getItem('orderInfo')){
                localStorage.removeItem('orderInfo');
            }
            var dataArr=[];
            var scIdArr=[];
            //将选中的课程scId存入数组
            angular.forEach($scope.shoppingList, function(data,index){
                angular.forEach($scope.shoppingList[index].cartList, function(info,i){
                    if(info.flag){
                        scIdArr.push(info.scId)
                    }
                });
            });
            //将选中的课程信息存入本地存储
            if($scope.all){
                dataArr=$scope.shoppingList;
            }else{
                angular.forEach($scope.shoppingList, function(data,index){
                    if(data.flag){
                        dataArr.push(data);
                    }else{
                        var dataFlag=true;
                        angular.forEach(data.cartList, function(info,i){
                            if(info.flag&&info.scId){
                                if(dataFlag){
                                    $scope.obj=data;
                                    $scope.obj.cartList=[];
                                    $scope.obj.cartList.push(info);
                                    dataFlag=false;
                                }else{
                                    $scope.obj.cartList.push(info);
                                }
                            }
                        });
                        dataArr.push($scope.obj);
                    }
                });
            }

            if(dataArr.length == 0){
                alert('请选择商品');
                return;
            }
            localStorage.setItem('orderInfo',JSON.stringify(dataArr));
            var req = {
                type:'POST',
                uri:'addOrder',
                data:{
                    scIds:scIdArr.join(',')
                }
            };
            httpServices.orderHttpService(req).success(function(response){
                $scope.$apply(function(){
                   $state.go('orderCtrl');
                })
            }).error(function(response){

            })
        };
        //删除订单请求
        function delectOrder(item){
            var req={
                type:'get',
                uri:'del',
                data:{
                    scIds:item
                }
            };
            httpServices.shoppingHttpService(req).success(function(data){
                if(data.code==0){
                    getShoppingList();
                }
            }).error(function(){
                console.log('删除数据失败')
            })
        }
        //删除单个课程
        $scope.delSingle=function(item){
            $scope.isDel=true;
            $scope.deleteData=item.scId;
        };

        //删除多个课程
        $scope.delectMore=function(){
            var arr=[];
            angular.forEach($scope.shoppingList, function(data,index){
                angular.forEach($scope.shoppingList[index].cartList, function(data,index){
                    if(data.flag){
                        arr.push(data.scId);
                    }
                });
            });
            if(arr.length==0){alert('请先选择删除课程');return}
            $scope.isDel=true;
            $scope.deleteData=arr.join(',');
        };
        //确认删除
        $scope.delOne=function(){
            $scope.isDel=false;
            delectOrder( $scope.deleteData)
        };
        //取消删除
        $scope.cancelDel=function(){
            $scope.isDel=false;
        };
    })
});
